package de.uni_mannheim.informatik.ai.dame_ai.main.ui;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

import de.uni_mannheim.informatik.ai.dame_ai.dame.ui.BitmapComponent;
import de.uni_mannheim.informatik.ai.dame_ai.main.ui.MainMenuDictionary.Phrases;


public class ObservablePanel extends JPanel {
	JFrame frameReference;
	BitmapComponent bitmap;
	JLayeredPane pane;
	MainMenuDictionary mainDict = MainMenuDictionary.getDefaultDictionary();
	JButton langButton = new JButton(mainDict.translate(Phrases.main_language));
	BufferedImage returnImg;
	Border raisedbevel = BorderFactory.createEtchedBorder();
	Border loweredbevel = BorderFactory.createEtchedBorder();
	Border compound = BorderFactory.createCompoundBorder(
			 raisedbevel, loweredbevel);
	public ObservablePanel(int width, int height,File imagePath) {
		try {
			langButton.setBounds(5, 5, 100, 20);
			langButton.setVisible(true);
			langButton.addActionListener(new LanguageButtonListener());
			setSize(width,height);
			System.out.println(imagePath.getAbsolutePath());
			BufferedImage img = ImageIO.read(new File(imagePath.getAbsolutePath()+ File.separator+"bg3.png"));
			returnImg = ImageIO.read(new File("media/main/main_return.png"));
			bitmap = new BitmapComponent(img, getBounds());
			pane = new JLayeredPane();
			setLayout(null);
			pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
			pane.setBounds(getBounds());
			pane.add(bitmap, new Integer(0));
			pane.add(langButton, new Integer(1));
			add(pane);
			bitmap.setNewBounds(pane.getBounds());
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		addComponentListener(new ComponentAdapter(){
			@Override	
			public void componentResized(ComponentEvent e){
				repaint();
			}
		});
	}
	public void calculateGeneralBounds(){
		pane.setBounds(getBounds());
		bitmap.setNewBounds(pane.getBounds());
	}
	 /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
	public Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
	@Override
	public void paint(Graphics g) {
		super.paint(g);
	}
	/*@Override
	public void setBounds(Rectangle arg0) {
		// TODO Auto-generated method stub
		super.setBounds(arg0);
		pane.setBounds(arg0);
		bitmap.setBounds(arg0);
	}*/
	public void setLanguageButton(int x, int y, int width, int height){
		pane.remove(langButton);
		langButton.setBounds(x, y, width, height);
		pane.add(langButton, new Integer(1));
	}
	public void addToPane(Component component){
		pane.add(component, new Integer(1));
	}
	protected void setPaneLayout(LayoutManager mgr){
		pane.setLayout(mgr);
	}
	protected ObservableObject observableObject = new ObservableObject();

	public void deleteObserver(Observer o) {
		observableObject.deleteObserver(o);
	}

	public void addObserver(Observer o) {
		observableObject.addObserver(o);
	}
	class ObservableObject extends Observable {
		ArrayList<Observer> observers = new ArrayList<Observer>();

		@Override
		public synchronized void addObserver(Observer o) {
			// TODO Auto-generated method stub
			observers.add(o);
		}

		Object result;

		public synchronized void setResult(Object result) {
			this.result = result;
		}

		@Override
		public synchronized void deleteObserver(Observer o) {
			observers.remove(o);
		}

		@Override
		public void notifyObservers() {
			ListIterator<Observer> it = observers.listIterator();
			while (it.hasNext()) {
				it.next().update(this, result);
			}
		}
	}
	class LanguageButtonListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			((MainMenuFrame)frameReference).changeLanguage();
		}
		
	}
}
